<?php namespace Home\Controller; 

use Hdphp\Controller\Controller;

//测试控制器
class IndexController extends Controller{

	//构造函数
	public function __init()
	{
	}
	
    //动作
    public function index(){
//        2(1)获取最新的文章
        $arcModel=new \Common\Model\Arc();
        $data=$arcModel->join('category','category_cid','=','cid')->where("is_recycle=0")
            ->orderBy('sendtime','DESC')->limit(5)->get();
        //    获取热门的文章
//        $arcModel=new \Common\Model\Arc();
        $datahot=$arcModel->join('category','category_cid','=','cid')->where("is_recycle=0")
            ->orderBy('click','DESC')->limit(5)->get();
//        p($data);exit;
//        (2)获得文章对应的标签
        $arcTagModel=new \Common\Model\Arctag();
        foreach($data as $k => $v){
//            给data追加字段{tag} 等于三张表关联
            $data[$k]['tag']=$arcTagModel->join('tag','tag_tid','=','tid')->where("article_aid={$v['aid']}")
                ->get();
        }

//        获得标签云
        $tagModel=new \Common\Model\Tag();
        $tagData=$tagModel->get();
        foreach($tagData as $k=>$v){
            $tagData[$k]['total']=$arcTagModel->join('article','article_aid','=','aid')->where("tag_tid={$v['tid']}")->where("is_recycle=0")->count();
        }
        View::with('tagData',$tagData);


//        p($data);exit;
        View::with('data',$data);
        View::with('datahot',$datahot);
//        3右侧的分类
        $cateModel=new \Common\Model\Cate();
//        获得所有的分类数据
        $cateData=$cateModel->get();
        foreach($cateData as $k=>$u){
//            select count(*) from arcitle where category_cid={$u['cid']}
            $cateData[$k]['total']=$arcModel->where("category_cid={$u['cid']}")->count();
        }
        View::with('cateData',$cateData);
//        p($cateData);exit;


       View::make();
    }
}
